package extractionStockageDonnees;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * 
 * @author simon
 * 
 */
public class ParserRunner {
	// A Runner's List
	private ArrayList<Runner> runners;
	// XML document to parse
	private Document documentXML;
	// XML document's root
	private Element root;

	/**
	 * Constructor
	 * 
	 * @param fileName
	 */
	public ParserRunner(String pathFile) {
		// Make a new SAXBuilder object
		SAXBuilder sxb = new SAXBuilder();
		try {
			// Make a new JDOM document with the complete data's path
			this.documentXML = sxb.build(new File(pathFile));
		} catch (Exception e) {
			e.printStackTrace();
		}
		// Initialize the document root
		this.root = documentXML.getRootElement();
		// Initialize the list of runners
		this.runners = new ArrayList<Runner>();

	}

	/**
	 * Method to retrieve all runners in the xml document
	 * 
	 * @return
	 */
	public ArrayList<Runner> parseRunners() {
		// Initialize the xml document's root
		root = documentXML.getRootElement();
		// Make an elements' list
		List<?> listElement = root.getChildren("runner");
		// Make an iterator to scroll the runners' list
		Iterator<?> iterElem = listElement.iterator();

		// ***** We loop *****
		while (iterElem.hasNext()) {
			// Current element (for each loop)
			Element currentElement = (Element) iterElem.next();
			int currentNumber = Integer.parseInt(currentElement.getChild(
					"number").getValue());
			String currentName = currentElement.getChild("name").getValue();
			String currentSurname = currentElement.getChild("surname")
					.getValue();
			int currentMoutain = Integer.parseInt(currentElement.getChild(
					"mountain").getValue());
			int currentPlain = Integer.parseInt(currentElement
					.getChild("plain").getValue());
			int currentTime = Integer.parseInt(currentElement.getChild("time")
					.getValue());
			Runner myCurrentRunner = new Runner(currentNumber, currentName,
					currentSurname, currentMoutain, currentPlain, currentTime);
			// We check the validy of the runner's capacities before to add him
			// to the collection
			if (runnerIsValide(currentMoutain, currentPlain, currentTime)) {
				// We add the current runner.
				runners.add(myCurrentRunner);
				// Doping runner
			} else {
				System.out
						.println("Le coureur numero "
								+ currentNumber
								+ " a ete controle positif au test de dopage. Il ne participera pas a la course.");
				System.out.println("Le dopage est un danger !");
			}

		}
		return runners;
	}

	/**
	 * Method to check the capacity of the runner (sum <= 16)
	 * 
	 * @param mountain
	 * @param plain
	 * @param time
	 * @return
	 */
	public boolean runnerIsValide(int mountain, int plain, int time) {
		return ((mountain + plain + time) > 0)
				&& ((mountain + plain + time) < 16);
	}

	// ========================== GETTERS & SETTERS ==========================
	/**
	 * @return
	 */
	public Document getDocumentXML() {
		return documentXML;
	}

	/**
	 * @param documentXML
	 */
	public void setDocument(Document documentXML) {
		this.documentXML = documentXML;
	}

	/**
	 * @return
	 */
	public Element getRoot() {
		return root;
	}

}
